﻿

using System;
using Game;
using dotmob;
using UnityEngine;
using UnityEngine.UI;

public class GamePlayPanel : ShowHidable
{
    [SerializeField] private Text _lvlTxt;
    [SerializeField] private Text txtTutorial;

    private void Start()
    {
        _lvlTxt.text = $" 关卡\n {LevelManager.Instance.Level.no}";

        if(LevelManager.Instance.Level.no == 1)
        {
            txtTutorial.gameObject.SetActive(true);
        }
        else
        {
            txtTutorial.gameObject.SetActive(false);
        }
        //Debug.Log("Level :" + LevelManager.Instance.Level.no);
    }

    public void OnClickUndo()
    {
        LevelManager.Instance.OnClickUndo();
    }

    public void OnClickRestart()
    {
        GameManager.LoadGame(new LoadGameData
        {
            Level = LevelManager.Instance.Level,
            GameMode = LevelManager.Instance.GameMode,
        },false);
    }

    public void OnClickSkip()
    {
        //if (!AdsManager.IsVideoAvailable())
        //{
        //    SharedUIManager.PopUpPanel.ShowAsInfo("Notice", "Sorry no video ads available.Check your internet connection!");
        //    return;
        //}

        //SharedUIManager.PopUpPanel.ShowAsConfirmation("Skip Level","Do you want watch Video ads to skip this level", success =>
        //{
        //    if(!success)
        //        return;

        //    AdsManager.ShowVideoAds(true, s =>
        //    {
        //        if(!s)
        //            return;
        //        ResourceManager.CompleteLevel(LevelManager.Instance.GameMode, LevelManager.Instance.Level.no);
        //        UIManager.Instance.LoadNextLevel();
        //    });

        //});

        AdManager.Instance.ShowRewardedVideoAd(OnAdWatched, OnAdError);

        //string videoAdid = VideData.Instance.GetVide();
        //TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);

        ////创建激励视频关闭回调
        //r.OnClose += (bol, val) =>
        //{
        //    Debug.Log("关闭广告 ，是否已经看完" + bol);
        //    //看完广告获得提示
        //    if (bol)
        //    {
        //        ResourceManager.CompleteLevel(LevelManager.Instance.GameMode, LevelManager.Instance.Level.no);
        //        UIManager.Instance.LoadNextLevel();
        //        MaidianNew.MaidianBackCallback(" ");
        //        Debug.Log("广告看完");
        //    }
        //    else
        //    {
        //        Debug.Log("广告未看完");
        //    }
        //};

        ////创建报错回调  
        //r.OnError += (code, errorMessage) =>
        //{

        //    Debug.Log("code:" + code + "错误信息：" + errorMessage);
        //};

        ////展示广告（新版本必须调用，不然无法展示出来广告）
        //r.Show();
    }


    private void OnAdWatched()
    {
        ResourceManager.CompleteLevel(LevelManager.Instance.GameMode, LevelManager.Instance.Level.no);
        UIManager.Instance.LoadNextLevel();
        Debug.Log("广告观看完成");

    }

    private void OnAdError(string errorMessage)
    {
        Debug.LogError($"广告播放失败: {errorMessage}");
        // 加载 AdError 预制体
        GameObject adErrorPrefab = Resources.Load<GameObject>("AdError");
        if (adErrorPrefab != null)
        {
            // 实例化 AdError
            GameObject adErrorInstance = Instantiate(adErrorPrefab, transform.position, Quaternion.identity);
            // 1 秒后销毁实例
            Destroy(adErrorInstance, 1.5f);
        }
        else
        {
            Debug.LogError("AdError 预制体未找到，请确保其位于 Resources 文件夹中。");
        }
    }

    public void OnClickMenu()
    {
        //SharedUIManager.PopUpPanel.ShowAsConfirmation("PAUSE", "Are you sure want to exit the game?", success =>
        // {
        //     if (!success)
        //         return;

        //     GameManager.LoadScene("MainMenu");
        // });

        SharedUIManager.PausePanel.Show();

    }

    private void Update()
    {
    }
}